package com.qf.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.entity.TbThesisproposal;
import com.qf.service.TbThesisproposalService;
import com.qf.mapper.TbThesisproposalMapper;
import com.qf.utils.Result;
import com.qf.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 *
 */
@Service
public class TbThesisproposalServiceImpl extends ServiceImpl<TbThesisproposalMapper, TbThesisproposal>
    implements TbThesisproposalService{

    private final TbThesisproposalMapper tbThesisproposalMapper;

    public TbThesisproposalServiceImpl(TbThesisproposalMapper tbThesisproposalMapper) {
        this.tbThesisproposalMapper = tbThesisproposalMapper;
    }


    @Override
    public Result sendthesisproposal(MultipartFile file, String oname, Integer studentId, HttpSession session) {
        //上传文件
        String fileName = file.getOriginalFilename();
        String ext = fileName.substring(fileName.lastIndexOf("." + 1));
        String firstName = UUIDUtils.getUUID();
        fileName=firstName+"."+ext;
        String realPath = session.getServletContext().getRealPath("/file");
        File file1=new File(realPath+ File.separator+fileName);
        if(!file1.exists()){
            file1.mkdirs();
        }
        try {
            file.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        TbThesisproposal tbThesisproposal = new TbThesisproposal();
        tbThesisproposal.setOname(oname);
        tbThesisproposal.setOfile(fileName);
        tbThesisproposal.setStudentId(studentId);

        tbThesisproposalMapper.insert(tbThesisproposal);

        return Result.success("上传成功");
    }
}




